import * as ww from '@wecom/jssdk'
import {
	apiGetQwParam
} from '@/api/seting.js'
let jsApiList = ['selectExternalContact', "sendChatMessage"]
let weixin = {
	/**
	 * 初始化
	 * @param {Object} params 初始化参数
	 * @param {Array} jsApiList 数组
	 */
	init(jsApiList = jsApiList, callback) {
		return new Promise(async (resolve, reject) => {
			let params = await apiGetQwParam({
				apilist: jsApiList.join(','),
				url: window.location
			})
			console.log(params)
			// 注册应用信息
			ww.register({
				corpId: params.corpid,
				agentId: params.agentid,
				jsApiList,
				async getAgentConfigSignature() {
					return {
						timestamp: params.timestamp,
						nonceStr: params.nonceStr,
						signature: params.signature,
					};
				},
				async onAgentConfigSuccess() {
					resolve()
					typeof callback == 'function' && callback()
				},
				// onAgentConfigFail: function(res) {
				// 	uni.showToast({
				// 		title: 'agentConfig 失败回调',
				// 		icon: 'none'
				// 	});
				// }
			})
		})
	},
	selectExternalContact(callback) {
		// 可以立刻调用JS接口，无需等待ready回调
		ww.selectExternalContact({
			success(res) {
				typeof callback == 'function' && callback(res)
			}
		})
	},
	sendChatMessage(news, callback) {
		// 可以立刻调用JS接口，无需等待ready回调
		ww.sendChatMessage({
			msgtype: "news",
			enterChat: true,
			news,
			success: (res) => {
				typeof callback == 'function' && callback(res)
			}
		})
	}
}
export default weixin;